package com.lst.projectlib.component.CustomView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class MengbanView extends RelativeLayout {
	private Context mContext = null;
	private Paint mPaint = null;
	private Bitmap mBackBitmap = null;
	private RectF mRectSrc = null;
	private View mHighLightView = null;
	private Xfermode mXmode = null;
	private View mTipsView = null;
	private int dx = 0;
	private int dy = 0;
	private int width = 0;
	private int height = 0;
	private int resId = 0;
	private boolean mIsHighLight = true;
	private Canvas cvs = null;
	private boolean mIsClickDimArea = true;
	private int hlLfet = 0;
	private int hlTop = 0;
	private int hlRight = 0;
	private int hlBottom = 0;

	public MengbanView(Context context) {
		super(context);
		init(context);
	}

	public MengbanView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MengbanView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init(context);
	}

	private void init(Context context) {
		mContext = context;
		setWillNotDraw(false);

		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// 半透明黑色
		mPaint.setColor(Color.argb(160, 0, 0, 0));
		// 留下底层和上层图的非交集部分
		mXmode = new PorterDuffXfermode(Mode.DST_OUT);

		mRectSrc = new RectF();
	}

	public void setHighLightView(View view) {
		mHighLightView = view;
	}

	public void setHighLight(boolean isHighLight) {
		mIsHighLight = isHighLight;
	}

	public int getResId() {
		return resId;
	}

	/**
	 * @param resId
	 */
	public void setTipsViewInfo(int resId, int width, int height, int dx, int dy) {
		this.dx = dx;
		this.dy = dy;
		this.width = width;
		this.height = height;
		this.resId = resId;
	}

	/**
	 * 对高亮区域的位置大小进行微调
	 * 由于某些view，比如ImageView的真实大小和显示出来的图片大小是不一致的
	 */
	public void setHLightViewInfo(int left, int top, int right, int bottom) {
		this.hlLfet = left;
		this.hlTop = top;
		this.hlRight = right;
		this.hlBottom = bottom;
	}

	public void setIsCanClickDimArea(boolean isCan) {
		mIsClickDimArea = isCan;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (cvs == null) {
			mBackBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
					Config.ARGB_8888);
			cvs = new Canvas(mBackBitmap);
		}

		cvs.drawColor(Color.TRANSPARENT, Mode.CLEAR);

		cvs.drawColor(mPaint.getColor());

		if (mHighLightView != null && mHighLightView.getWidth() != 0
				&& mHighLightView.getHeight() != 0) {
			int[] location = new int[2];
			mHighLightView.getLocationInWindow(location);

			int[] loc = new int[2];
			this.getLocationInWindow(loc);

			int left = location[0] - loc[0];
			int top = location[1] - loc[1];

			mRectSrc.left = left + this.hlLfet;
			mRectSrc.top = top + this.hlTop;
			mRectSrc.right = left + mHighLightView.getWidth() + this.hlRight;
			mRectSrc.bottom = top + mHighLightView.getHeight() + this.hlBottom;

			if (mTipsView == null) {
				mTipsView = new View(mContext);
				mTipsView.setBackgroundResource(resId);
				LayoutParams lp = new LayoutParams(width, height);
				lp.topMargin = top - lp.height + dy;
				lp.leftMargin = left - lp.width / 2 + dx;
				this.addView(mTipsView, lp);
			} else {
				mTipsView.setBackgroundResource(resId);
				LayoutParams lp = (LayoutParams) mTipsView.getLayoutParams();
				lp.width = width;
				lp.height = height;
				lp.topMargin = top - lp.height + dy;
				lp.leftMargin = left - lp.width / 2 + dx;
			}
		}

		mPaint.setXfermode(mXmode);
		if (mRectSrc != null && cvs != null && mIsHighLight) {
			cvs.drawRect(mRectSrc, mPaint);
		}
		mPaint.setXfermode(null);

		canvas.drawBitmap(mBackBitmap, 0, 0, null);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (isTouchHighLightView(ev)) {
			this.setVisibility(View.GONE);
			return false;
		} else {
			if (!mIsClickDimArea) {
				return true;
			}
		}

		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (isTouchHighLightView(event)) {
			this.setVisibility(View.GONE);
			return false;
		} else {
			if (!mIsClickDimArea) {
				return true;
			}
		}

		return super.onTouchEvent(event);
	}

	private boolean isTouchHighLightView(MotionEvent ev) {
		boolean isTouch = false;

		if (mHighLightView != null && mIsHighLight) {
			int[] location = new int[2];
			mHighLightView.getLocationInWindow(location);

			int[] loc = new int[2];
			this.getLocationInWindow(loc);

			int left = location[0] - loc[0];
			int top = location[1] - loc[1];

			RectF rect = new RectF(left, top, left + mHighLightView.getWidth(),
					top + mHighLightView.getHeight());
			if (rect.contains(ev.getX(), ev.getY())) {
				isTouch = true;
			}
		}

		return isTouch;
	}
}
